dojo.provide("com.hayes.chromeRss.PopupController");

dojo.require("com.hayes.rss.Item");

dojo.declare(
  "com.hayes.chromeRss.PopupController",
  null,
  {
    wrapper: null,
    spinner: null,
    imgWrapper: null,
    imgFade: null,
    img: null,
    link: null,
    dropdown: null,
    feedItemArr: [],
    months: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
    selectedItem: null,
    rssUrl: "",
    titleTruncateLength: 26,
    
    constructor: function(rssUrl) {
      this.rssUrl = rssUrl;
      dojo.addOnLoad(dojo.hitch(this, "_onLoad"));
    },
    
    _onLoad: function(event) {
      spinner = dojo.byId('spinner');
      var xhrArgs = {
        url: this.rssUrl,
        handleAs: "xml",
        load: dojo.hitch(this, "_feedOnLoad"),
        error: dojo.hitch(this, "_feedError")
      };
      dojo.xhrGet(xhrArgs);
    },
    
    _feedOnLoad: function(data) {
      this._initLayout();
      this.hideSpinner();
      this._loadFeedItems(data.getElementsByTagName('item'));
      this._populateDateDropDown();
      this.setSelected(this.feedItemArr[0]);
    },
    
    /**
     * Populates the combobox at the top of the popup window
     */
    _populateDateDropDown: function() {
      dojo.forEach(this.feedItemArr, dojo.hitch(this, function(item, idx){
        var optn = document.createElement("option");
        var title = item.title;
        if(this.titleTruncateLength < item.title.length) { // title needs to be truncated
          title = title.substring(0, this.titleTruncateLength) + "...";
        } 
        optn.text = this.formatDate(item.pubDate) + ' - ' + title;
        optn.value = idx;
        this.dropdown.options.add(optn);
      }));
    },
    
    setSelected: function(item, fade) {
      this.selectedItem = item;
      var tempHTML = dojo.create("div");
      tempHTML.innerHTML = item.description;
      var imgSrc = tempHTML.getElementsByTagName('img')[0].src;
      var href = tempHTML.getElementsByTagName('a')[0].href;
      
      if(fade) {
        dojo.style(this.imgFade, "display", "block");
        dojo.attr(this.imgFade, "src", dojo.attr(this.img, "src"));
        var fadeArgs = {
          node: this.imgFade
        }
        var anim = dojo.fadeOut({node: this.imgFade});
        dojo.connect(anim, "onEnd", dojo.hitch(this, "_fadeOutOnEnd"));
        anim.play();
      }
      
      
      dojo.attr(this.link, "href", href);
      dojo.attr(this.img, "src", imgSrc);
    },
    
    _fadeOutOnEnd: function() {
      dojo.style(this.imgFade, "display", "none");
      dojo.style(this.imgFade, "opacity", "1");
    },
    
    _loadFeedItems: function(rssItems) {
      dojo.forEach(rssItems, dojo.hitch(this, function(rssItem) {
        try {
          var feedItem = new com.hayes.rss.Item(rssItem);
          this.feedItemArr.push(feedItem);  
        } catch(e){}
      }));
    },
    
    _feedError: function(error) {
      dojo.attr(content, "innerHTML", "Currently unavailable");
    },
    
    _initLayout: function() {
      this.wrapper = dojo.create("div", {className: "wrapper"}, dojo.body());
      this.dropdown = dojo.create("select", null, this.wrapper);
      this.imgWrapper = dojo.create("div", {className: "imgWrapper"}, this.wrapper);
      this.link = dojo.create("a", {
        href: "",
        target: "_blank"
      }, this.imgWrapper);
      this.imgFade = dojo.create("img", {className: "imgFade", style: "display: none;"}, this.link); 
      this.img = dojo.create("img", {}, this.link);
      
      // event handlers
      dojo.connect(this.link, "onclick", dojo.hitch(this, "linkOnClick"));
      dojo.connect(this.dropdown, "onchange", dojo.hitch(this, "dropdownOnChange"));
    },
    
    linkOnClick: function(event) {
      this.closeWindow();
    },
    
    closeWindow: function() {
      window.close();
    },
    
    dropdownOnChange: function(event) {
      this.setSelected(this.feedItemArr[event.currentTarget.selectedIndex], true);
    },
    
    formatDate: function(date) {
      return date.getDate() + " " + this.months[date.getMonth()];
    },
    
    showSpinner: function() {
      dojo.style(spinner, "display", "auto");
    },
    
    hideSpinner: function() {
      dojo.style(spinner, "display", "none");
    }
    
  }
);